package com.dave.gd.common.model;

import java.util.ArrayList;
import java.util.Date;

import com.dave.gd.common.util.ObjectUtils;

/**
 * 
 * 直接使用List不太方便,采用ListBean代替
 * 
 * @author chillming
 */
public class ListBean extends ArrayList<Object> {
    private static final long serialVersionUID = -478241990557313639L;
    
    public ListBean() {
        super();
    }
    
    public ListBean(int initialCapacity) {
        super(initialCapacity);
    }
    
    public int getInt(int index) {
        if (isOutOfBounds(index)) {
            return 0;
        }
        return getInt(index, 0);
    }
    
    public int getInt(int index, int defaultValue) {
        if (isOutOfBounds(index)) {
            return defaultValue;
        }
        return ObjectUtils.toInt(get(index), defaultValue);
    }
    
    public long getLong(int index) {
        if (isOutOfBounds(index)) {
            return 0;
        }
        return getLong(index, 0);
    }
    
    public long getLong(int index, long defaultValue) {
        if (isOutOfBounds(index)) {
            return defaultValue;
        }
        return ObjectUtils.toLong(get(index), defaultValue);
    }
    
    public double getDouble(int index) {
        if (isOutOfBounds(index)) {
            return 0;
        }
        return getDouble(index, 0);
    }
    
    public double getDouble(int index, double defaultValue) {
        if (isOutOfBounds(index)) {
            return defaultValue;
        }
        return ObjectUtils.toDouble(get(index), defaultValue);
    }
    
    public String getString(int index) {
        return getString(index, null);
    }
    
    public String getString(int index, String defaultValue) {
        if (isOutOfBounds(index)) {
            return defaultValue;
        }
        return ObjectUtils.toString(get(index), defaultValue);
    }
    
    public Date getDate(int index) {
        if (isOutOfBounds(index)) {
            return null;
        }
        return ObjectUtils.toDate(get(index));
    }
    
    private boolean isOutOfBounds(int index) {
        return (index < 0 || index >= size());
    }
}
